package com.hs.mod_archive.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.hs.framework.base.BaseDataBindFragment
import com.hs.mod_archive.databinding.FragmentOptLogBinding
import com.hs.mod_archive.helper.ListViewHelper
import com.hs.mod_archive.helper.ListViewHelper.Companion.perPageCount
import com.hs.mod_archive.helper.OptLogManagerHelper
import com.hs.room.model.archive.OptLog
import com.hs.room.repository.service.ILogRepository
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 用户操作日志
 */
@AndroidEntryPoint
class FragmentOptLog :
    BaseDataBindFragment<FragmentOptLogBinding>() {

    @Inject
    lateinit var service: ILogRepository

    private var datas: List<OptLog>? = null
    lateinit var helper: OptLogManagerHelper
    override fun initView(view: View, savedInstanceState: Bundle?) {
        helper =
            OptLogManagerHelper(
                mBinding!!.footPageListView,
                null,
                object : ListViewHelper.FootSearchCallback {
                    override fun onActionSearch(index: Int) {
                        goPage(index)
                    }
                })
    }


    private fun goPage(i: Int) {
        lifecycleScope.launch {
            service.getOptLog(i, perPageCount).collect {
                datas = it
                helper.setListDataAndRefresh(it)
            }
        }
    }


    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            helper.setFootValue(service.countOptLog(), 0)
        }
        goPage(0)
    }


    override fun onDestroy() {
        super.onDestroy()
        helper.onDestroyView()
    }
}